﻿using System;
using System.Collections.Generic;
using HRM.Model;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace HRM.ViewModel
{
    public class WorkingHistoryDialogViewModel : ViewModelGeneric<QuaTrinhCongTac>
    {
        private IEntityService<ChucVu> _regencyService;
        private IEntityService<PhongBan> _departmentService;

        public WorkingHistoryDialogViewModel(IEntityService<QuaTrinhCongTac> service, IEntityService<ChucVu> regencyService, IEntityService<PhongBan> departmentSevice)
            : base(service)
        {
            if (SimpleIoc.Default.IsRegistered<NhanVien>()) Staff = SimpleIoc.Default.GetInstance<NhanVien>();
            else
            {
                throw new Exception("Staff not found");
            }
            _regencyService = regencyService;
            _departmentService = departmentSevice;
            Regencies = _regencyService.GetAll();
            Departments = _departmentService.GetAll();
            if (Regencies.Count > 0) RegencySelected = Regencies[0];
            if (Departments.Count > 0) DepartmentSelected = Departments[0];
        }

        public NhanVien Staff { get; private set; }

        public string Title
        {
            get { return string.Format("Quản lý quá trình công tác - {0}", Staff.HoTen); }
        }

        public List<ChucVu> Regencies { get; private set; }

        private ChucVu _regencySelected;

        public ChucVu RegencySelected
        {
            get { return _regencySelected; }
            set
            {
                if (_regencySelected != value)
                {
                    _regencySelected = value;
                    RaisePropertyChanged("RegencySelected");
                }
            }
        }


        private PhongBan _departmentSelected;

        public PhongBan DepartmentSelected
        {
            get { return _departmentSelected; }
            set
            {
                if (_departmentSelected != value)
                {
                    _departmentSelected = value;
                    RaisePropertyChanged("DepartmentSelected");
                }
            }
        }

        public List<PhongBan> Departments { get; private set; }

        public override QuaTrinhCongTac SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    if (value != null)
                    {
                        DepartmentSelected = Departments.Find(x => x.MaPhongBan == SelectedItem.MaPhongBan);
                        RegencySelected = Regencies.Find(x => x.MaChucVu == SelectedItem.MaChucVu);
                    }
                    RaisePropertyChanged("SelectedItem");
                }
            }
        }

        public override RelayCommand SaveCommand
        {
            get
            {
                return new RelayCommand(() =>
                    {
                        if (SelectedItem == null) return;
                        if (DepartmentSelected == null || RegencySelected == null) return;
                        SelectedItem.MaNhanVien = Staff.MaNhanVien;
                        SelectedItem.MaChucVu = RegencySelected.MaChucVu;
                        SelectedItem.MaPhongBan = DepartmentSelected.MaPhongBan;
                        if (AddMode) _service.Add(SelectedItem);
                        else _service.Update(SelectedItem);
                        Refresh();
                    });
            }
        }
    }
}
